package com.xxl.job.admin.api.controller;

import cn.hutool.core.lang.UUID;
import com.xxl.job.admin.api.model.XxlJobApiApp;
import com.xxl.job.admin.api.service.XxlJobApiAppService;
import com.xxl.job.admin.controller.annotation.PermissionLimit;
import com.xxl.job.core.biz.model.ReturnT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/adminApi")
public class ApiAdminController {
    @Autowired
    private XxlJobApiAppService xxlJobApiAppService;

    @RequestMapping
    @PermissionLimit(adminuser = true)
    public String index(Model model) {
        return "api/api.index";
    }


    @RequestMapping("/pageList")
    @ResponseBody
    @PermissionLimit(adminuser = true)
    public Map<String, Object> pageList(HttpServletRequest request,
                                        @RequestParam(required = false, defaultValue = "0") int start,
                                        @RequestParam(required = false, defaultValue = "10") int length,
                                        String appname, String title) {

        // page query
        List<XxlJobApiApp> list = xxlJobApiAppService.pageList(start, length, appname);
        int list_count = xxlJobApiAppService.pageListCount(start, length, appname);

        // package result
        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("recordsTotal", list_count);		// 总记录数
        maps.put("recordsFiltered", list_count);	// 过滤后的总记录数
        maps.put("data", list);  					// 分页列表
        return maps;
    }

    @RequestMapping("/save")
    @ResponseBody
    @PermissionLimit(adminuser = true)
    public ReturnT<String> save(XxlJobApiApp xxlJobGroup){

        // valid
        if (xxlJobGroup.getAppName()==null || xxlJobGroup.getAppName().trim().length()==0) {
            return new ReturnT<String>(500, "请输入AppName" );
        }
        if (xxlJobGroup.getAppId().length()<3 || xxlJobGroup.getAppId().length()>64) {
            return new ReturnT<String>(500, "AppId 长度 3-64之间" );
        }

        if (xxlJobGroup.getAppKey()==null || xxlJobGroup.getAppKey().trim().length()==0) {
            xxlJobGroup.setAppKey(UUID.randomUUID().toString().replace("-","").toUpperCase());
        }

        if(xxlJobGroup.getEnable() == null ||
                xxlJobGroup.getEnable() != 0 ||
                xxlJobGroup.getEnable() != 1
        ){
            xxlJobGroup.setEnable(1);
        }

        int ret = xxlJobApiAppService.save(xxlJobGroup);
        return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
    }

    @RequestMapping("/update")
    @ResponseBody
    @PermissionLimit(adminuser = true)
    public ReturnT<String> update(XxlJobApiApp xxlJobGroup){
        if (xxlJobGroup.getId() <= 0) {
            return new ReturnT<String>(500, "请输入ID" );
        }

        // process
        xxlJobGroup.setUpdateTime(new Date());

        int ret = xxlJobApiAppService.update(xxlJobGroup);
        return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
    }

    @RequestMapping("/remove")
    @ResponseBody
    @PermissionLimit(adminuser = true)
    public ReturnT<String> remove(int id){

        int ret = xxlJobApiAppService.remove(id);
        return (ret>0)?ReturnT.SUCCESS:ReturnT.FAIL;
    }
}
